package com.harmay.center.monitor.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 *
 * @author Zuper
 * @date 2022/2/22 17:50
 */
public class OkHttpUtils {
    private final static MediaType contentType = MediaType.parse("application/json; charset=utf-8");

    private final static Map<String, List<Cookie>> cookieMap = new HashMap();

    private static OkHttpClient client;

    private static ObjectMapper jackson = JsonUtils.jackson();

    static {
        client = new OkHttpClient.Builder().cookieJar(new CookieJar() {
                    public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                        cookieMap.put(url.host(), cookies);
                    }

                    public List<Cookie> loadForRequest(HttpUrl url) {
                        List<Cookie> cookies = cookieMap.get(url.host());
                        return cookies != null ? cookies : new ArrayList<>();
                    }
                })
                .connectTimeout(60 * 1000L, TimeUnit.MILLISECONDS)
                .readTimeout(30 * 60 * 1000L, TimeUnit.MILLISECONDS)
                .writeTimeout(30 * 60 * 1000L, TimeUnit.MILLISECONDS)
                .build();
    }

    public static String post(String param, String url) throws IOException {
        RequestBody requestBody = FormBody.create(contentType, param);
        Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        Map<String, Object> responseMap = jackson.readValue(response.body().string(), Map.class);
        return jackson.writeValueAsString(responseMap);
    }

    public static String postWithHeader(String param, String url, String token) throws IOException {
        RequestBody requestBody = FormBody.create(contentType, param);
        Request request = new Request.Builder()
                .post(requestBody).header("Content-Type", "application/json").header("Authorization", token)
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        Map<String, Object> responseMap = jackson.readValue(response.body().string(), Map.class);
        return jackson.writeValueAsString(responseMap);
    }

    public static String postWithHeader1(String param, String url) throws IOException {
        RequestBody requestBody = FormBody.create(contentType, param);
        Request request = new Request.Builder()
                .post(requestBody).header("Content-Type", "application/json")
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        Map<String, Object> responseMap = jackson.readValue(response.body().string(), Map.class);
        return jackson.writeValueAsString(responseMap);
    }

    public static Map<String, Object> get(String url) throws IOException {

        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        Map<String, Object> responseMap = jackson.readValue(response.body().string(), Map.class);
        return responseMap;
    }
}
